﻿using MetroDataApi.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace MetroDataApi.Controllers.Management
{
    public class MetroProjectGroupController : ApiController
    {


        // GET api/metroprojectgroup/5
        public string Get(int prjId)
        {
            using (var db = new MetroDataDataContext())
            {
                var list = new MetroProjectGroup[] { };
                var g = db.MetroProjectGroup.FirstOrDefault(p => p.PrjId == prjId);
                if (g == null)
                {
                   
                }
                else
                {
                    list = db.MetroProjectGroup.Where(p => p.GroupId == g.GroupId && p.PrjId != prjId).ToArray();
                }
                return JsonConvert.SerializeObject(list);
            }
        }

        // POST api/metroprojectgroup
        public string Post([FromBody]string value)
        {
            JObject jsonData = JObject.Parse(value);
            var bindPrjName = jsonData["prjName"].ToString();
            int prjId = jsonData["prjId"].ToObject<int>();
            using (var db = new MetroDataDataContext())
            {
                var g = db.MetroProjectGroup.FirstOrDefault(p => p.PrjId == prjId);
                var bindPrj = db.Projects.Single(p => p.Name == bindPrjName);
                var prj = db.Projects.Single(p => p.PrjId == prjId);
                var maxGroupId = -1;
                if (g == null)
                {
                    var mx=db.MetroProjectGroup.Max(p => p.GroupId);
                    maxGroupId = mx.HasValue ? mx.Value + 1 : 1;
                    var self = new MetroProjectGroup()
                    {
                        GroupId = maxGroupId,
                        PrjId = prjId,
                        PrjName = prj.Name
                    };

                    db.MetroProjectGroup.InsertOnSubmit(self);

                }
                else
                {
                    maxGroupId = g.GroupId.Value;
                }
                var exsited = (from p in db.MetroProjectGroup
                               where p.GroupId == maxGroupId
                                   && p.PrjName == bindPrjName
                               select p).SingleOrDefault();
                //避免重复添加
                if (exsited == null)
                {
                    var bind = new MetroProjectGroup()
                    {
                        GroupId = maxGroupId,
                        PrjId = bindPrj.PrjId,
                        PrjName = bindPrj.Name
                    };
                    db.MetroProjectGroup.InsertOnSubmit(bind);
                }
                db.SubmitChanges();
                return "true";
            }
        }


        // DELETE api/metroprojectgroup/5
        public string Delete(int prjId,string prjName)
        {
            using (var db = new MetroDataDataContext())
            {
                 var g = db.MetroProjectGroup.First(p => p.PrjId == prjId);
                 var list = db.MetroProjectGroup.Where(p =>
                     p.GroupId == g.GroupId);
                 var target = list.SingleOrDefault(p => p.PrjName == prjName);
                 if (target != null)
                 {
                     if (list.Count() <= 2)
                     {
                         db.MetroProjectGroup.DeleteAllOnSubmit(list);
                     }
                     else
                     {
                         db.MetroProjectGroup.DeleteOnSubmit(target);
                     }
                     db.SubmitChanges();
                 }
         
                 return "true";
            }
        }
    }
}
